package com.dilean.impl.model;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.dilean.api.model.Conversation;
import com.dilean.api.model.Message;
import com.dilean.api.model.Person;
import java.text.SimpleDateFormat;

@Entity
@Table(name = "MESSAGES")
public class MessageImpl extends AbstractEntityImpl implements Message {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DILEAN_ID_SEQ")
    @SequenceGenerator(name = "DILEAN_ID_SEQ", sequenceName = "DILEAN_ID_SEQ", allocationSize = 1)
    @Column(name = "ID")
    private Integer id;

    @Column(name = "NAME", unique = false, nullable = false)
    private String name;

    @Override
    public Integer getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne(targetEntity = PersonImpl.class)
    @JoinColumn(name = "SENDER")
    private Person sender;

    @Column(name = "DATA", unique = false, nullable = false)
    private String data;

    @Column(name = "\"DATE\"", unique = false, nullable = true)
    private Calendar date;

    @Column(name = "PRIORITY", unique = false, nullable = true)
    private Integer priority;

    @ManyToOne(targetEntity = ConversationImpl.class)
    @JoinColumn(name = "CONVERSATION_ID")
    private Conversation conversation;

    @Column(name = "ORDER_NUMBER", unique = true, nullable = false)
    private int orderNumber;

    public MessageImpl() {

    }

    public MessageImpl(String data, Person sender, Conversation conversation) {
        this.data = data;
        this.sender = sender;
        this.conversation = conversation;
        this.date = new GregorianCalendar();
    }

    @SuppressWarnings("unused")
    @PrePersist
    @PreUpdate
    private void prepareIndex() {
        if (conversation != null) {
            orderNumber = conversation.getMessages().size();
        }
    }

    @Override
    public Person getSender() {
        return this.sender;
    }

    @Override
    public String getData() {
        return data;
    }

    @Override
    public Calendar getDate() {
        return date;
    }
    
    @Override
    public String getDateStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("d MMM YYYY, HH:mm:ss");
        return sdf.format(date.getTime());
    }
    

    @Override
    public Integer getPriority() {
        return priority;
    }

    @Override
    public Conversation getConversation() {
        return this.conversation;
    }

    @Override
    public int getOrderNumber() {
        return orderNumber;
    }

    @Override
    public void setSender(Person person) {
        this.sender = person;
    }

    @Override
    public void setData(String data) {
        this.data = data;
    }

    @Override
    public void setDate(Calendar date) {
        this.date = date;
    }

    @Override
    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    @Override
    public void setConversation(Conversation conversation) {
        this.conversation = conversation;
    }

    @Override
    public void setOrderNumber(int order) {
        this.orderNumber = order;
    }

}
